package iosr11.gbase.dao;

import iosr11.data.Group;
import iosr11.data.User;
import iosr11.data.UserGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;

public class UserGroupDAOImpl extends GenericDAOImpl<UserGroup> implements UserGroupDAO{

	private static String TYPE = "system user group";
	private static String USER_REF = "user";
	private static String GROUP_REF = "group";
	private Logger log = Logger.getLogger("service-log");
	
	@Override
	public List<UserGroup> getUserGroupForGroup(Group entity) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault()
				.getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance()
				.getGoogleBaseQueryResult(query, GROUP_REF,
						entity.getId());
		if (feed == null) {
			log.info("brak rekordow w bazie majacych referencje do grupy: "+entity.getTitle());
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak userow dla grupy: "+entity.getTitle());
			return null;
		}
		List<UserGroup> result = new ArrayList<UserGroup>();
		for (GoogleBaseEntry entry : feed.getEntries()){
			UserGroup tmp = this.rewrite(entry);
			if (tmp!=null)
				result.add(tmp);
		}
		return result;
	}

	@Override
	public List<UserGroup> getUserGroupForUser(User entity) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault()
				.getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance()
				.getGoogleBaseQueryResult(query, USER_REF,
						entity.getId());
		if (feed == null) {
			log.info("brak rekordow w bazie majacych referencje do usera: "+entity.getName());
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak grup dla usera: "+entity.getName());
			return null;
		}
		List<UserGroup> result = new ArrayList<UserGroup>();
		for (GoogleBaseEntry entry : feed.getEntries()){
			UserGroup tmp = this.rewrite(entry);
			if (tmp!=null)
				result.add(tmp);
		}
		return result;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	@Override
	public UserGroup rewrite(GoogleBaseEntry entry) {
		UserGroup entity = new UserGroup();
		entity.setId(entry.getId());
		Group group = new Group();
		group.setId(entry.getGoogleBaseAttributes().getTextAttribute(GROUP_REF));
		entity.setGroup(group);
		User user = new User();
		user.setId(entry.getGoogleBaseAttributes().getTextAttribute(USER_REF));
		return entity;
	}

	@Override
	public GoogleBaseEntry rewrite(UserGroup entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.setDraft(true);
		
		entry.getGoogleBaseAttributes().setItemType(TYPE);
		entry.getGoogleBaseAttributes().addTextAttribute(USER_REF, entity.getUser().getId());
		entry.getGoogleBaseAttributes().addTextAttribute(GROUP_REF, entity.getGroup().getId());
		return entry;
	}

}
